agentmux_srv\backend\reactive/
mod.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Reactive messaging system for agent-to-agent terminal communication.
5//! Port of Go's pkg/reactive/.
6//!
7//! Provides message injection into terminal panes, agent registration,
8//! rate limiting, message sanitization, audit logging, and cross-host
9//! polling via AgentMux cloud service.
10
11pub mod handler;
12pub mod poller;
13pub mod registry;
14pub mod sanitize;
15pub mod types;
16#[cfg(test)]
17mod tests;
18
19use std::time::{SystemTime, UNIX_EPOCH};
20
21// ---- Constants ----
22
23/// Maximum message length in bytes.
24pub const MAX_MESSAGE_LENGTH: usize = 10_000;
25
26/// Suffix appended when a message is truncated.
27pub const TRUNCATION_SUFFIX: &str = "\n[Message truncated]";
28
29/// Maximum entries in the audit log ring buffer.
30const AUDIT_LOG_MAX: usize = 100;
31
32/// Rate limit: max tokens (requests per second).
33const RATE_LIMIT_MAX: u32 = 10;
34
35/// Default poll interval for AgentMux poller (seconds).
36pub const DEFAULT_POLL_INTERVAL_SECS: u64 = 30;
37
38// ---- Re-exports ----
39
40#[allow(unused_imports)]
41pub use handler::{get_global_handler, Handler, ReactiveHandler};
42#[allow(unused_imports)]
43pub use poller::Poller;
44#[allow(unused_imports)]
45pub use sanitize::{
46    format_injected_message, sanitize_message, validate_agent_id, validate_agentmux_url,
47};
48#[allow(unused_imports)]
49pub use types::*;
50
51// ---- Helpers ----
52
53/// Get current time as Unix milliseconds.
54fn now_unix_millis() -> u64 {
55    SystemTime::now()
56        .duration_since(UNIX_EPOCH)
57        .unwrap_or_default()
58        .as_millis() as u64
59}
60
61/// Compute SHA-256 hex digest of a string (for audit log privacy).
62fn sha256_hex(input: &str) -> String {
63    use std::collections::hash_map::DefaultHasher;
64    use std::hash::{Hash, Hasher};
65    // Use a fast non-crypto hash for audit log (privacy, not security)
66    let mut hasher = DefaultHasher::new();
67    input.hash(&mut hasher);
68    format!("{:016x}", hasher.finish())
69}